package com.zf.shuashua.ui.mine;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zf.shuashua.BR;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Constants;
import com.zf.shuashua.base.BaseViewModel;
import com.zf.shuashua.base.BasicActivity;
import com.zf.shuashua.databinding.ActivityBindPhoneBinding;
import com.zf.shuashua.ui.gift.H5Activity;
import com.zf.shuashua.utils.TextUtil;

public class BindPhoneActivity extends BasicActivity<ActivityBindPhoneBinding, BaseViewModel> {

    private String phoneNUmber, name, iconUrl;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public int getViewModelId() {
        return BR.viewModel;
    }

    @Override
    public BaseViewModel getViewModel() {
        return ViewModelProviders.of(this).get(BaseViewModel.class);
    }

    @Override
    public void setupView() {
        binding.imgBack.setOnClickListener(v -> {
            finish();
        });
        binding.tvtoProtocol.setOnClickListener(v -> {
            jumpToProtocol();
        });
        binding.tvtoPolicy.setOnClickListener(v -> {
            jumpToPolicy();
        });
        binding.tvNext.setOnClickListener(v -> {
            jumpToCode();
        });
        setupPhone();
    }

    @Override
    public void initData() {
        super.initData();
        name = getIntent().getStringExtra("name");
        binding.tvName.setText(name);

        String type = getIntent().getStringExtra("type");
        String thridLogin = "微博";
        if("wechat".equals(type)){
            thridLogin = "微信";
        }else if("qq".equals(type)){
            thridLogin = "qq";
        }
        binding.tvTip.setText(String.format(getResources().getString(R.string.bind_phone_tips), thridLogin));

        iconUrl = getIntent().getStringExtra("iconUrl");
        Glide.with(this).load(iconUrl)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))//圆形
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(binding.imgAvatar);
    }

    private void setupPhone(){
        binding.phoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s) && TextUtil.isMobile(s.toString())) {
                    binding.tvNext.setEnabled(true);
                    phoneNUmber = s.toString();
                } else {
                    binding.tvNext.setEnabled(false);
                    phoneNUmber = "";
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void jumpToProtocol(){
        Intent intent = new Intent(this, H5Activity.class);
        intent.putExtra("money", Constants.H5_URL + "protocol");
        intent.putExtra("title", "长豆用户协议");
        startActivity(intent);
    }

    private void jumpToPolicy(){
        Intent intent = new Intent(this, H5Activity.class);
        intent.putExtra("money", Constants.H5_URL + "policy");
        intent.putExtra("title", "长豆隐私政策");
        startActivity(intent);
    }

    private void jumpToCode(){
        if (TextUtil.isMobile(phoneNUmber)) {
            Intent intent = new Intent(this, VerifyActivity.class);
            intent.putExtra("phoneNumber", phoneNUmber);
            intent.putExtra("name", name);
            intent.putExtra("iconUrl", iconUrl);
            startActivity(intent);
        }
    }

}
